﻿using M_Process.API.Appliction.Command.ProcessTasksCommand;
using M_Process.Domain;
using M_Process.Repository.ProcessImpl;
using M_Process.Repository.ProcessInterface;
using MediatR;

namespace M_Process.API.Appliction.CommandHandlers.ProcessTasksCommandHandler
{
    /// <summary>
    /// 创建工序任务命令处理器
    /// </summary>
    public class CreateProcessTasksCommandHandler : IRequestHandler<CreateProcessTasksCommand, int>
    {
        /// <summary>
        /// 工序任务仓储接口
        /// </summary>
        protected readonly IProcessTasksRepository processTasksRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="processTasksRepository">工序任务仓储接口</param>
        public CreateProcessTasksCommandHandler(IProcessTasksRepository processTasksRepository)
        {
            this.processTasksRepository = processTasksRepository;
        }

        /// <summary>
        /// 处理创建工序任务命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(CreateProcessTasksCommand request, CancellationToken cancellationToken)
        {
            return await processTasksRepository.AddAsync(new ProcessTasks()
            {
                TaskNumber = request.TaskNumber,
                TaskStatus = "未开始",
                Priority = request.Priority,
                ProductionBatch = request.ProductionBatch,
                ProcessName = request.ProcessName,
                ProductNumber = request.ProductNumber,
                CreatedBy = "张三",
                CreatedDate = DateTime.Now
            });
        }
    }
}
